layui.use(['form',], function () {
    let form = layui.form,
        layer = layui.layer,
        $ = layui.$;


    //监听提交
    form.on('submit(saveBtn)', function (data) {

        let  postdata =data.field

        //发异步，把数据提交给php
        $.ajax({
            url: '/admin/industry/Savele?action=ajax',
            type: 'post',
            dataType: 'json',
            data:postdata,
            success: function(res) {
                if (res.status == true) {
                    layer.msg('保存成功', {icon: 1, time: 3000},
                        function(){
                            parent.location.reload();
                        })
                    //let iframeIndex = parent.layer.getFrameIndex(window.name);
                    //parent.layer.close(iframeIndex);
                }  else {
                    layer.open({
                        title: '错误信息',
                        content: '<span style="color:red">'+res.code+':</span>'+res.msg
                    })
                    return false
                }
            },
            error: function(re) {
                console.log(re)
                layer.open({
                    title: '错误信息',
                    content: '<span style="color:red">'+re.responseJSON.errorCode+':</span>'+re.responseJSON.msg
                })
            }
        });




    });

   //监听指定开关
    form.on('switch(switchTest)', function(data){

        let is_limits=$("#bb").attr('data-display');

        if( this.checked){
            $("#bb").attr('style','display:block')
        }else{
            $("#bb").attr('style','display:none')
        }
        // layer.msg('开关checked：'+ (this.checked ? 'true' : 'false'), {
        //     offset: '6px'
        // });

    });
});


/*点击添加限制*/
function additem(){



    let level_name=$("#bb .layui-form-item").length
    level_name+=1
    let option=''

    for (var a=1;a<13;a++)
    {
        option+='<option   value="'+a+'">'+a+'级</option>';
    }

    let new_html = '<div class="layui-form-item " >\n' +
        '            <label class="layui-form-label" style="margin-left:50px;width:50px">限制'+level_name+'</label>\n' +
        '            <div class="layui-inline" >\n' +
        '                <label class="layui-form-label">级别</label>\n' +
        '                <div class="layui-input-block ">\n' +
        '                    <select name="member_level[]"   >\n' + option+'</select>\n' +
        '                </div>\n' +
        '            </div>\n' +
        '            <div class="layui-inline" >\n' +
        '                <label class="layui-form-label">人数</label>\n' +
        '                <div class="layui-input-inline">\n' +
        '                    <input type="number" name="member_num[]"   value="0"      class="layui-input cycle_income"  >\n' +
        '                </div>\n' +
        '            </div>\n' +
        '            <div class="layui-inline" style="width: 50px">\n' +
        '                <div class="layui-input-inline">\n' +
        '                <button type="button" onclick="delcycle(this)" class="layui-btn">删除</button>\n' +
        '                </div>\n' +
        '            </div>\n' +
        '        </div>'

    $('#bb').append(new_html);
    layui.form.render()

}


function delcycle (obj){
    $(obj).parents('.layui-form-item').remove();
}